<?php
/**
 * Created by PhpStorm.
 * User: wdt
 * Date: 2016/3/21
 * Time: 16:34
 */

namespace front\modules\app\controllers;


use coupons\services\CouponsGroupService;
use front\controllers\WebBaseController;

class CouponsController extends WebBaseController {

    /**
     * 获取卡券信息即获取卡券组信息
     */
    public function actionGetInfo()
    {
        $coupons_group_id = \Yii::$app->request->post('coupons_group_id');
        if (empty($coupons_group_id)) {
            WebBaseController::getAjaxData(2,'未传递卡券组ID');
        }
        $ret = CouponsGroupService::getGroupInfo($coupons_group_id);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'获取卡券信息成功');
        }
        WebBaseController::getAjaxData(3,'获取卡券信息失败');
    }

    /**
     * 领取卡券
     */
    public function actionReceive()
    {
        $coupons_group_id = \Yii::$app->request->post('coupons_group_id');
        $user_id = \Yii::$app->request->post('user_id');
        if (empty($coupons_group_id)) {
            WebBaseController::getAjaxData(2,'未传递卡券组ID');
        }
        if (empty($user_id)) {
            WebBaseController::getAjaxData(3,'未传递用户ID');
        }
        $ret = CouponsGroupService::receive($coupons_group_id,$user_id);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'领取卡券成功');
        }
        WebBaseController::getAjaxData(4,'领券卡券失败');
    }

    /**
     * 获取用户不同状态卡券列表
     */
    public function actionCouponsList()
    {
        $user_id = \Yii::$app->request->post('user_id');
        $status = \Yii::$app->request->post('status');
        if (empty($user_id)) {
            WebBaseController::getAjaxData(2,'未传递用户ID');
        }
        if (empty($status)) {
            WebBaseController::getAjaxData(3,'未传递卡券状态');
        }
        $ret = CouponsGroupService::getStatus($user_id,$status);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,$ret);
        }
        WebBaseController::getAjaxData(4,'获取用户卡券列表失败');
    }

    /**
     * 使用卡券
     */
    public function actionUse()
    {
        $data = array();
        $data['group_id'] = \Yii::$app->request->post('group_id');
        $data['order_id'] = \Yii::$app->request->post('order_id');
        $data['user_id'] = \Yii::$app->request->post('user_id');
        if ((empty($data['coupons_id'])) && (empty($data['order_id']) && (empty($data['user_id'])))) {
            WebBaseController::getAjaxData(2,'必传参数未传递');
        }
        if (!empty($data['value'])) {
            WebBaseController::getAjaxData(1,CouponsGroupService::pay($data['user_id'],$data['coupons_id'],$data['order_id'],$data['value']));
        }
        if (!empty($data['percent'])) {
            WebBaseController::getAjaxData(1,CouponsGroupService::pay($data['user_id'],$data['coupons_id'],$data['order_id'],$data['percent']));
        }
        if (!empty($data['gift_product_id'])) {
            WebBaseController::getAjaxData(1,CouponsGroupService::pay($data['user_id'],$data['coupons_id'],$data['order_id'],$data['gift_product_id']));
        }
    }
}